package com.sugar.queryinfo.module.business.controller.admin.dataquery;

import cn.hutool.core.util.DesensitizedUtil;
import com.sugar.queryinfo.framework.idempotent.core.annotation.Idempotent;
import com.sugar.queryinfo.framework.idempotent.core.keyresolver.impl.UserIdempotentKeyResolver;
import com.sugar.queryinfo.framework.security.core.util.SecurityFrameworkUtils;
import com.sugar.queryinfo.module.business.controller.admin.dataquery.vo.audit.DataQueryAuditSimpleRespVO;
import com.sugar.queryinfo.module.business.controller.admin.dataquery.vo.audit.DataQuerySaveAuditReqVO;
import com.sugar.queryinfo.module.business.controller.admin.dataquery.vo.audit.DataQuerySaveAuditRespVO;
import com.sugar.queryinfo.module.business.util.HideUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.queryinfo.framework.common.pojo.PageParam;
import com.sugar.queryinfo.framework.common.pojo.PageResult;
import com.sugar.queryinfo.framework.common.pojo.CommonResult;
import com.sugar.queryinfo.framework.common.util.object.BeanUtils;
import static com.sugar.queryinfo.framework.common.pojo.CommonResult.success;

import com.sugar.queryinfo.framework.excel.core.util.ExcelUtils;

import com.sugar.queryinfo.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.queryinfo.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.queryinfo.module.business.controller.admin.dataquery.vo.*;
import com.sugar.queryinfo.module.business.dal.dataobject.dataquery.DataQueryDO;
import com.sugar.queryinfo.module.business.service.dataquery.DataQueryService;

@Tag(name = "管理后台 - 数据查询")
@RestController
@RequestMapping("/business/data-query")
@Validated
public class DataQueryController {

    @Resource
    private DataQueryService dataQueryService;

    @PostMapping("/create")
    @Operation(summary = "创建数据查询")
    @PreAuthorize("@ss.hasPermission('business:data-query:create')")
    @Idempotent(timeout = 3, message = "您的手速太快啦！", keyResolver = UserIdempotentKeyResolver.class)
    public CommonResult<Long> createDataQuery(@Valid @RequestBody DataQuerySaveReqVO createReqVO) {
        return success(dataQueryService.createDataQuery(createReqVO,
                SecurityFrameworkUtils.getLoginUserId(), SecurityFrameworkUtils.getLoginUserDeptId()));
    }

    /*@PutMapping("/update")
    @Operation(summary = "更新数据查询")
    @PreAuthorize("@ss.hasPermission('business:data-query:update')")
    public CommonResult<Boolean> updateDataQuery(@Valid @RequestBody DataQuerySaveReqVO updateReqVO) {
        dataQueryService.updateDataQuery(updateReqVO);
        return success(true);
    }*/

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据查询")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:data-query:delete')")
    public CommonResult<Boolean> deleteDataQuery(@RequestParam("id") Long id) {
        dataQueryService.deleteDataQuery(id);
        return success(true);
    }

    @DeleteMapping("/delete/all")
    @Operation(summary = "删除全部")
    @PreAuthorize("@ss.hasPermission('business:data-query:delete')")
    public CommonResult<Boolean> deleteDataQueryAll() {
        dataQueryService.deleteDataQueryAll();
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据查询")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:data-query:query')")
    public CommonResult<DataQueryRespVO> getDataQuery(@RequestParam("id") Long id) {
        return success(dataQueryService.getDataQuery(id));
    }

    @PostMapping("/refresh")
    @Operation(summary = "刷新某个数据")
    @PreAuthorize("@ss.hasPermission('business:data-query:query')")
    @Idempotent(timeout = 3, message = "您的手速太快啦！", keyResolver = UserIdempotentKeyResolver.class)
    public CommonResult<Object> refreshData(@Valid @RequestBody DataQueryRefreshReqVO reqVO) {
        return success(dataQueryService.refreshData(reqVO));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据查询分页")
    @PreAuthorize("@ss.hasPermission('business:data-query:query')")
    public CommonResult<PageResult<DataQueryRespVO>> getDataQueryPage(@Valid DataQueryPageReqVO pageReqVO) {
        PageResult<DataQueryDO> pageResult = dataQueryService.getDataQueryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DataQueryRespVO.class, o ->
                o.setQueryName(HideUtil.name(o.getQueryName()))
                    .setQueryIdCard(HideUtil.idCard(o.getQueryIdCard()))
                    .setQueryMobile(HideUtil.mobile(o.getQueryName()))));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据查询 Excel")
    @PreAuthorize("@ss.hasPermission('business:data-query:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDataQueryExcel(@Valid DataQueryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DataQueryDO> list = dataQueryService.getDataQueryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数据查询.xls", "数据", DataQueryRespVO.class,
                        BeanUtils.toBean(list, DataQueryRespVO.class));
    }

    @PostMapping("/blacklist/{id}/{remark}")
    @Operation(summary = "加入黑名单")
    @PreAuthorize("@ss.hasPermission('business:data-query:create')")
    public CommonResult<Boolean> dataQueryAuditAddBlacklist(@PathVariable("id")Long id, @PathVariable("remark")String remark) {
        dataQueryService.dataQueryAuditAddBlacklist(id, remark);
        return success(true);
    }



    @PostMapping("/create/audit/query")
    @Operation(summary = "创建只能审核数据查询")
    @PreAuthorize("@ss.hasPermission('business:data-query:create')")
    public CommonResult<Long> createAuditDataQuery(@Valid @RequestBody DataQueryAuditSaveReqVO auditSaveReqVO) {
        return success(dataQueryService.createAuditDataQuery(auditSaveReqVO,
                SecurityFrameworkUtils.getLoginUserId(), SecurityFrameworkUtils.getLoginUserDeptId()));
    }

    @PostMapping("/create/audit")
    @Operation(summary = "创建智能审核")
    //@PreAuthorize("@ss.hasPermission('business:info-audit:create')")
    public CommonResult<DataQuerySaveAuditRespVO> createDataQueryAudit(@Valid @RequestBody DataQuerySaveAuditReqVO createReqVO) {
        return success(dataQueryService.createDataQueryAudit(createReqVO.getContent(),
                SecurityFrameworkUtils.getLoginUserId(), SecurityFrameworkUtils.getLoginUserDeptId()));
    }

    @DeleteMapping("/delete/audit")
    @Operation(summary = "删除智能审核")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:info-audit:delete')")
    public CommonResult<Boolean> deleteDataQueryAudit(@RequestParam("id") Long id) {
        dataQueryService.deleteDataQuery(id);
        return success(true);
    }

    @GetMapping("/get/audit/{id}")
    @Operation(summary = "获得智能审核")
    @PreAuthorize("@ss.hasPermission('business:info-audit:query')")
    public CommonResult<DataQuerySaveAuditRespVO> getDataQueryAudit(@PathVariable("id")Long id) {
        return success(dataQueryService.getDataQueryAudit(id));
    }

    @GetMapping("/page/audit")
    @Operation(summary = "获得智能审核分页")
    @PreAuthorize("@ss.hasPermission('business:info-audit:query')")
    public CommonResult<PageResult<DataQueryAuditSimpleRespVO>> getDataQueryPageAudit(@Valid DataQueryPageReqVO pageReqVO) {
        PageResult<DataQueryAuditSimpleRespVO> pageResult = dataQueryService.getDataQueryPageAudit(pageReqVO);
        for (DataQueryAuditSimpleRespVO respVO : pageResult.getList()) {
            respVO.setQueryName(HideUtil.name(respVO.getQueryName()))
                    .setQueryIdCard(HideUtil.idCard(respVO.getQueryIdCard()));
        }
        return success(pageResult);
    }
}